﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Nirvana.SoundCommon
{
    /// <summary>
    /// 控制audiosource的音量作淡入/淡出播放
    /// </summary>
    public sealed class AudioSourceCtrl
    {
        private AudioSource audioSource;
        private float delaytime;
        private float fadeinTime;
        private float fadeoutTime;
        private float startTime;
        private float stopTime;
        private float volume;

        public float DelayTime { get { return delaytime; } set { delaytime = value; } }
        public float FadeInTime { get { return fadeinTime; } set { fadeinTime = value; } }
        public float FadeOutTime { get { return fadeoutTime; } set { fadeoutTime = value; } }

        public AudioSourceCtrl(AudioSource audioSource, float delay = 0f, float fadein = 0f, float fadeout = 0f)
        {
            this.audioSource = audioSource;
            this.delaytime = delay;
            this.fadeinTime = fadein;
            this.fadeoutTime = fadeout;
        }

        public bool IsPlaying()
        {
            return this.audioSource != null && this.audioSource.isPlaying;
        }

        public bool IsStop()
        {
            return this.stopTime > 0;
        }
        /// <summary>
        /// 剩余时间
        /// </summary>
        /// <returns></returns>
        public float GetRamingTime()
        {
            return GetClipLength() - GetPlayingTime();
        }

        /// <summary>
        /// 获取当前音已频播放了多长时间
        /// </summary>
        /// <returns></returns>
        public float GetPlayingTime()
        {
            return this.audioSource.time;
        }
        /// <summary>
        /// 音频总时长
        /// </summary>
        /// <returns></returns>
        public float GetClipLength()
        {
            return this.audioSource.clip.length;
        }

        public void Play()
        {
            this.startTime = Time.realtimeSinceStartup;
            this.stopTime = -1;
            this.volume = this.audioSource.volume;
            if (delaytime > 0)
                this.audioSource.PlayDelayed(delaytime);
            else
                this.audioSource.Play();
        }

        public void Stop()
        {
            if (fadeoutTime > 0)
                this.stopTime = Time.realtimeSinceStartup;
            else
            {
                if (this.audioSource != null)
                {
                    this.audioSource.Stop();
                    this.audioSource = null;
                }
            }
        }

        /// <summary>
        /// 更新音效音量的淡入，淡出
        /// </summary>
        public void UpdateVolume()
        {
            if (startTime > 0)
            {
                float curTime = this.FadeInTime - (Time.realtimeSinceStartup - this.startTime);
                if (curTime > 0)
                {
                    //递增
                    this.audioSource.volume = this.volume * (1 - curTime / this.FadeInTime);

                }
                else
                {
                    this.audioSource.volume = this.volume;
                    this.startTime = -1f;
                }
            }

            if (stopTime > 0)
            {
                float curTime = fadeoutTime - (Time.realtimeSinceStartup - stopTime);
                if (curTime <= 0)
                {
                    if (this.audioSource != null)
                    {
                        this.audioSource.Stop();
                        this.audioSource = null;
                    }
                    return;
                }
                //衰减 
                this.audioSource.volume = this.volume * curTime / this.fadeoutTime;
            }
        }

    }
}